package com.googlecode.webduff.methods;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.googlecode.webduff.WebdavStatus;
import com.googlecode.webduff.exceptions.MethodResponseError;


public class DoUnlock extends AbstractMethod {
	
	private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(DoUnlock.class);
	
	public MethodResponse execute(HttpServletRequest req, HttpServletResponse resp) throws IOException, MethodResponseError {
		MethodResponse theResponse = new MethodResponse(resp);
		String lockId = getLockIdFromRequest(req);
		
		if(lockId != null && resourceLocks.getLockByID(lockId) != null) {
			resourceLocks.unlock(lockId, "system.WebDuff");
			theResponse.setStatusCode(WebdavStatus.SC_NO_CONTENT);
		} else {
			log.warn("Wrong/invalid locktoken sent!");
		}
		
		return theResponse;
	}

}